
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.commonutils.redis;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 *
 * @author Rainbow
 */
//单机REDIS工具类
public class JedisPoolUtil
{
	
	//日志
	protected static Logger logger = LoggerFactory.getLogger(JedisPoolUtil.class);
    public static volatile JedisPool jedisPool;
    public JedisPoolUtil(String ip ,int port)
    {
        init(ip, port);
    }

    public synchronized static void init(String ip ,int port)
    {
        System.out.println("init jedis pool");
        if (jedisPool == null)
        {
            // redis 属性配置
            JedisPoolConfig config = new JedisPoolConfig();
            config.setBlockWhenExhausted(true); // 连接耗尽则阻塞
            config.setLifo(true); //后进先出
            config.setMaxTotal(5000); // 最大连接数
            config.setMaxIdle(256);  // 最小空闲连接数
            config.setMaxWaitMillis(-1); // 设置最大等待毫秒数，小于0时为阻塞不确定的时间
            config.setTestOnBorrow(true); // 获取连接时是否检查连接的有效性：是
            config.setTestOnReturn(true); // 归还连接时是否检查连接的有效性：是
            config.setTestWhileIdle(true); // 空闲时是否检查连接的有效性：是
            config.setMinEvictableIdleTimeMillis(600000L); // 逐出连接的最小空闲时间：10分钟
            config.setTimeBetweenEvictionRunsMillis(30000L); //逐出扫描的时间间隔(毫秒) 30秒
            config.setNumTestsPerEvictionRun(5); //每次逐出检查时 逐出的最大数目 5条
            jedisPool = new JedisPool(config, ip, port);
        }
    }
      
    public static Jedis getJedis() 
    {
        return jedisPool.getResource();
    }

    public static String get(String key)
    {
        String value = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
        return value;
    }

    public static void close(Jedis jedis)
    {
        try
        {
            jedisPool.returnResource(jedis);
        } catch (Exception e)
        {
            if (jedis.isConnected())
            {
                jedis.quit();
                jedis.disconnect();
            }
        }
    }

    public static byte[] get(byte[] key)
    {
        byte[] value = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }

        return value;
    }

    public static void set(String key, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static void set(String key, String value, int time)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            jedis.expire(key, time);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static void set(byte[] key, byte[] value, int time)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            jedis.expire(key, time);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static void hset(byte[] key, byte[] field, byte[] value)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.hset(key, field, value);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }
    //redis写入对象，用序列化对象的方式
    public  void hset(String filed,String key, Object Obj) {
    	Jedis jedis = null;
    	 jedis = jedisPool.getResource();
        jedis.hset(filed.getBytes(), key.getBytes(),SerializeUtil.serialize(Obj));
    }

    public static void hset(String key, String field, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.hset(key, field, value);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static String hget(String key, String field)
    {
        String value = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            value = jedis.hget(key, field);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }

        return value;
    }

    public static byte[] hget(byte[] key, byte[] field)
    {
        byte[] value = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            value = jedis.hget(key, field);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }

        return value;
    }

    public static void hdel(byte[] key, byte[] field)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.hdel(key, new byte[][]
            {
                field
            });
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static void lpush(String key, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.lpush(key, new String[]
            {
                value
            });
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static void rpush(byte[] key, byte[] value)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.rpush(key, new byte[][]
            {
                value
            });
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static void rpoplpush(byte[] key, byte[] destination)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.rpoplpush(key, destination);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static List<byte[]> lpopList(byte[] key)
    {
        List list = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            list = jedis.lrange(key, 0L, -1L);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }

        return list;
    }

    public static String rpop(String key)
    {
        String bytes = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            bytes = jedis.rpop(key);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }

        return bytes;
    }

    public static List<byte[]> lrange(byte[] key, int from, int to)
    {
        List result = null;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            result = jedis.lrange(key, from, to);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }

        return result;
    }

    public static void del(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
    }

    public static long llen(byte[] key)
    {
        long len = 0L;
        Jedis jedis = null;
        try
        {
            jedis = jedisPool.getResource();
            jedis.llen(key);
        } catch (Exception e)
        {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally
        {
            close(jedis);
        }
        return len;
    }
}

